﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace IBS
{
    public partial class IbsDataContext
    {
        /// <summary>
        /// Сохранение поставки в базу IBS
        /// </summary>
        /// <param name="delivery"></param>
        public Int64 SaveSaleOrder(Interfaces.ISaleOrder SaleOrder)
        {
            var headerId = SaveSaleHeader(SaleOrder);
            SaleOrder.SaleOrderItems.ToList().ForEach(r => SaveSaleItem(r, headerId));

            return headerId;
        }

        private long SaveSaleHeader(Interfaces.ISaleOrder SaleOrder)
        {
            using (var connection = new OracleConnection(this.connectionString))
            {
                connection.Open();
                using (var command = new OracleCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "EDEX.USP_ADD_SaleOrder";
                    command.Parameters.Clear();
                    command.Parameters.Add("p_ID_RECORD", OracleType.Number).Direction = ParameterDirection.Output;
                    command.Parameters.AddWithValue("p_OUTER_KEY", SaleOrder.OUTER_KEY.ToString());
                    command.Parameters.AddWithValue("P_DOC_NUMBER", SaleOrder.DOC_NUMBER);
                    command.Parameters.AddWithValue("p_DOC_DATE", SaleOrder.DOC_DATE);
                    command.Parameters.AddWithValue("p_COMPANY_KEY", SaleOrder.COMPANY_KEY);
                    command.Parameters.AddWithValue("p_LOCATION_KEY", SaleOrder.LOCATION_KEY);
                    command.Parameters.AddWithValue("p_STATUS", 0);
                    command.Parameters.AddWithValue("p_LINECOUNT", SaleOrder.LINECOUNT);
                    command.Parameters.AddWithValue("p_TOTAL_AMOUNT", SaleOrder.TOTAL_AMOUNT);
                    command.Parameters.AddWithValue("p_TAX_AMOUNT", SaleOrder.TAX_AMOUNT);
                    command.Parameters.AddWithValue("p_NOTE", SaleOrder.NOTE);
                    command.ExecuteNonQuery();

                    return Convert.ToInt64(command.Parameters["p_ID_RECORD"].Value);
                }
            }
        }

        private void SaveSaleItem(Interfaces.ISaleOrderLine item, long headerId)
        {
            using (var connection = new OracleConnection(this.connectionString))
            {
                connection.Open();
                using (var command = new OracleCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "EDEX.USP_ADD_SaleOrderLine";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("p_ID_INVOICE", headerId);
                    command.Parameters.AddWithValue("P_ITEM_KEY", item.ITEM_KEY);
                    command.Parameters.AddWithValue("p_PRICE",item.PRICE);
                    command.Parameters.AddWithValue("p_ACTUAL_PRICE", item.ACTUAL_PRICE);
                    command.Parameters.AddWithValue("p_DISCOUNT", DBNull.Value);
                    command.Parameters.AddWithValue("p_QUANTITY", Math.Abs(item.QUANTITY));
                    command.Parameters.AddWithValue("p_TOTAL", item.TOTAL);                    
                    command.ExecuteNonQuery();
                }
            }
        }

    }
}